package cn.jjxx.modules.iface.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.entity.Column;

import cn.jjxx.core.model.AjaxJson;
import cn.jjxx.core.query.wrapper.EntityWrapper;
import cn.jjxx.core.utils.StringUtils;
import cn.jjxx.modules.sys.entity.Attachment;
import cn.jjxx.modules.sys.entity.Organization;
import cn.jjxx.modules.sys.service.IAttachmentService;
import cn.jjxx.modules.sys.service.IOrganizationService;

@Controller
@RequestMapping("/iface/sys")
@Api(tags = "系统管理",description="系统管理部分接口说明",hidden=true)
public class SysController {

	@Autowired
    protected IOrganizationService organizationService;
	@Autowired
    protected IAttachmentService attachmentService;
	
	/**
	 * @Description: 获取当前用户组织信息 .<br>
	 * @param types 组织类型，可以是多个.<br>
	 * @author 郑成功 .<br>
	 * @date 2018-10-16 下午5:34:15.<br>
	 */
	@RequestMapping(value = "/org/getOrgs",method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	@ApiOperation(value="获取所有权限组织",notes="根据组织类型，获取所有权限组织")
	@ApiImplicitParams({
		@ApiImplicitParam(name="types",value="控制类型数组,多个用英文都好隔开(1:组织,2:公司,3:项目,4:项目办,5:标段,6:部门,7:总包,8:正式,9:临时)",
				dataType="String",allowableValues="1,2,3,4,5,6,7,8,9",allowMultiple=true, paramType = "query"),
	})
	@ApiResponse(response=AjaxJson.class, code = 200, message = "接口返回对象参数")
	public AjaxJson getOrgs(String[] types){
		AjaxJson j = new AjaxJson();
		List<Organization> list = organizationService.getPermisOrgs(types);
		j.setData(list);
		return j;
	}
	
	/**
	 * @Description: 获取当前用户组织信息 .<br>
	 * @param types 组织类型，可以是多个.<br>
	 * @author 郑成功 .<br>
	 * @date 2018-10-16 下午5:34:15.<br>
	 */
	@RequestMapping(value = "/attachment/getAttachments",method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	@ApiOperation(value="获取单据附件",notes="根据单据Id,获取附件信息")
	@ApiImplicitParams({
		@ApiImplicitParam(name="billId",value="单据Id",dataType="String",required=true, paramType = "query"),
	})
	@ApiResponse(response=AjaxJson.class, code = 200, message = "接口返回对象参数")
	public AjaxJson getAttachments(String billId){
		AjaxJson j = new AjaxJson(); 
		EntityWrapper<Attachment> wrapper = new EntityWrapper<Attachment>(Attachment.class);
		Column id = Column.create().column("id");
		Column filename = Column.create().column("filename");
		Column fileext = Column.create().column("fileext");
		Column filepath = Column.create().column("filepath");
		wrapper.setSqlSelect(id,filename,fileext,filepath);
		wrapper.eq("billId", billId);
		List<Attachment> list = attachmentService.selectList(wrapper);
		j.setData(list);
		return j;
	}
	
	/**
	 * @Description: 删除附件 .<br>
	 * @param id 附件Id.<br>
	 * @author 郑成功 .<br>
	 * @date 2018-11-12 下午5:04:57.<br>
	 */
	@RequestMapping(value = "/attachment/delete",method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	@ApiOperation(value="删除附件",notes="根据附件id,删除附件")
	@ApiImplicitParams({
		@ApiImplicitParam(name="ids",value="附件Id,多个用英文逗号隔开",dataType="String",required=true, paramType = "query"),
	})
	@ApiResponse(response=AjaxJson.class, code = 200, message = "接口返回对象参数")
	public AjaxJson delAttachment(String ids){
		AjaxJson j = new AjaxJson(); 
		j.setMsg("附件删除成功");
		if(StringUtils.isNotEmpty(ids)){
			List<String> idList = Arrays.asList(ids.split(","));
			boolean flag = attachmentService.deleteBatchIds(idList);
			if(!flag){
				j.setRet(AjaxJson.RET_FAIL);
				j.setMsg("附件删除失败");
			}
		}else{
			j.setRet(AjaxJson.RET_FAIL);
			j.setMsg("附件删除失败");
		}
		return j;
	}
}
